package com.cs.beneficios.web.action;

import com.cs.beneficios.business.entity.RepDetBOTDAO;
import com.cs.beneficios.business.model.RepDetBOTVO;
import com.cs.beneficios.common.ConnectionFactory;

import java.io.IOException;

import java.sql.SQLException;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ReporteFinalEGAction extends Action {
	/**This is the main action called from the Struts framework.
	 * @param mapping The ActionMapping used to select this instance.
	 * @param form The optional ActionForm bean for this request.
	 * @param request The HTTP Request we are processing.
	 * @param response The HTTP Response we are processing.
	 */
	public ActionForward execute(
			ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response
			) throws IOException, ServletException {

		HttpSession session = request.getSession();
		String subpro = (String)session.getAttribute("generar");
		String sede = (String)session.getAttribute("sede");
		List lGeneral = new LinkedList();
		List lEspecifico = new LinkedList();

		lGeneral = (List)session.getAttribute("listageneral");
		lEspecifico = (List)session.getAttribute("listaEspecificos");
		List lFiltro = (List)session.getAttribute("ListaFiltrado");
		String fechaInicial = (String)session.getAttribute("fechaInicial");
		String fechaFinal = (String)session.getAttribute("fechaFinal");
		String codigoAtrGestor = (String)session.getAttribute("codigoAtrGestor");
		String codigovfl = (String)session.getAttribute("codigovfl");

		if (lGeneral.size() != 0 || lEspecifico.size() != 0) {

			int itamanoEsp = lEspecifico.size();
			String tamanoEsp = String.valueOf(itamanoEsp);
			int itamanoGen = lGeneral.size();
			String tamanoGen = String.valueOf(itamanoGen);

			session.setAttribute("TamanoEsp", tamanoEsp);
			session.setAttribute("TamanoGen", tamanoGen);

			ConnectionFactory cf = new ConnectionFactory();
			RepDetBOTVO criterea = new RepDetBOTVO();
			criterea.setCodeSede(new Long(sede));
			criterea.setCodeSubprograma(new Long(subpro));

			if (fechaInicial != null && fechaFinal != null && !fechaInicial.equalsIgnoreCase("") && 
				!fechaFinal.equalsIgnoreCase("")) {
				criterea.setDesde(fechaInicial);
				criterea.setHasta(fechaFinal);
			}
			if (codigoAtrGestor != null && codigovfl != null) {
				criterea.setCodigoAtrGestor(codigoAtrGestor);
				criterea.setGestor(codigovfl);
			}

			RepDetBOTDAO BOTDAO = new RepDetBOTDAO(cf);

			try {
				//cantidadBenOtor = BOTDAO.getCantidadBeneOtorg(criterea);
				criterea.setCantidadBeneficiarios(BOTDAO.getCantidadBeneficiarios(lFiltro, cf, criterea));
				criterea.setValor(BOTDAO.getValorTotal(lFiltro, cf, criterea));
				session.setAttribute("Parametros", criterea);
				List reporte = (List)BOTDAO.getReporteConGestor(lGeneral, lEspecifico, lFiltro, cf, criterea);
				criterea.setEntregas(new Integer(reporte.size()));
				BOTDAO.getFilters(criterea);
				cf.closeConnection(BOTDAO.getConn());
				session.setAttribute("listareporte", (Object)reporte);
			} catch (SQLException e) {
				System.err.println("/******** TDSUB - cwsub");
				e.printStackTrace();
				System.err.println("TDSUB - cwsub ********/");
			}

		} else {
			request.setAttribute(
					"msg", "Seleccione alguna opcion en las listas de atributos del beneficio o del beneficiario"
				);
		}

		return mapping.findForward("success");
	}
}
